package com.gitee.wsl.android.ui.ext

//package com.dylanc.longan

import android.app.Activity
import android.app.KeyguardManager
import android.content.Context
import android.content.ContextWrapper
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.provider.Settings
import android.view.Surface
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import com.gitee.wsl.android.ext.ApplicationInit.application
import com.gitee.wsl.android.ext.store.contentResolver
import timber.log.Timber


object ScreenExt{
    inline val screenWidth: Int get() = application.resources.displayMetrics.widthPixels

    inline val screenHeight: Int get() = application.resources.displayMetrics.heightPixels

    /**
     * 切换屏幕方向
     * @param activity [Activity]
     * @return `true` 横屏, `false` 竖屏
     */
    fun toggleScreenOrientation(activity: Activity): Boolean {
        try {
            // 判断是否竖屏
            return if (activity.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
                activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                true // 切换横屏, 并且表示属于横屏
            } else {
                activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
                false // 切换竖屏, 并且表示属于竖屏
            }
        } catch (e: Exception) {
            Timber.d( e, "toggleScreenOrientation")
        }
        return false
    }

    // =
    /**
     * 获取屏幕旋转角度
     * @param activity [Activity]
     * @return 屏幕旋转角度
     */
    fun getScreenRotation(activity: Activity): Int {
        try {
            when (activity.windowManager.defaultDisplay.rotation) {
                Surface.ROTATION_0 -> return 0
                Surface.ROTATION_90 -> return 90
                Surface.ROTATION_180 -> return 180
                Surface.ROTATION_270 -> return 270
            }
        } catch (e: Exception) {
           Timber.d( e, "getScreenRotation")
        }
        return 0
    }

    /**
     * 判断是否锁屏
     * @return `true` yes, `false` no
     */
    fun isScreenLock(): Boolean {
        try {
            val keyguardManager: KeyguardManager = application.getSystemService(Context.KEYGUARD_SERVICE) as KeyguardManager
            return keyguardManager.inKeyguardRestrictedInputMode()
        } catch (e: java.lang.Exception) {
            Timber.d( e, "isScreenLock")
        }
        return false
    }



}


inline var Fragment.isFullScreen: Boolean
    get() = activity?.isFullScreen == true
    set(value) {
        activity?.isFullScreen = value
    }

inline var Activity.isFullScreen: Boolean
    get() = window.decorView.rootWindowInsetsCompat?.isVisible(WindowInsetsCompat.Type.systemBars()) == true
    set(value) {
        window.decorView.windowInsetsControllerCompat?.run {
            val systemBars = WindowInsetsCompat.Type.systemBars()
            if (value) show(systemBars) else hide(systemBars)
        }
    }

inline var Fragment.isLandscape: Boolean
    get() = activity?.isLandscape == true
    set(value) {
        activity?.isLandscape = value
    }

inline var Activity.isLandscape: Boolean
    get() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
    set(value) {
        requestedOrientation = if (value) {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        } else {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }

inline var Fragment.isPortrait: Boolean
    get() = activity?.isPortrait == true
    set(value) {
        activity?.isPortrait = value
    }

inline var Activity.isPortrait: Boolean
    get() = resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT
    set(value) {
        requestedOrientation = if (value) {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
    }

val Context.activity: Activity?
    get() {
        var context: Context? = this
        while (context is ContextWrapper) {
            if (context is Activity) {
                return context
            }
            context = context.baseContext
        }
        return null
    }

var systemScreenTimeout :Int
   get() {
       return Settings.System.getInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT)
   }
   set(value) {
        try {
           Settings.System.putInt(contentResolver, Settings.System.SCREEN_OFF_TIMEOUT, value)
       } catch (e: SecurityException) {
           Timber.e("Error writing screen timeout", e)
       }
   }